<template>
  <div class="left-content" v-if="examStore.currQuestion?.titleList?.length && justOne">
    <div
      class="content-box"
      v-if="!tableOfTitle"
      v-html="examStore.currQuestion?.titleList?.[0]?.title"
    ></div>

    <!-- 表格渲染 -->
    <div class="question-title" v-else-if="tableOfTitle.length">
      <template v-for="item in tableOfTitle">
        <span
          v-if="item !== '<p>{【表格插入位置标识】}</p>'"
          v-html="mathHTMLToShow(item)"
        ></span>
        <el-table
          v-if="item == '<p>{【表格插入位置标识】}</p>'"
          ref="excelRef"
          style="width: 100%"
          :data="table?.[0]?.data?.slice(1)"
          border
        >
          <el-table-column
            :label="table?.[0]?.data?.[0]?.[it]"
            :prop="it"
            v-for="(it, ind) in table?.[0]?.filed"
            sortable
          /><template #empty>
            <div class="custom-loading">
              <el-icon class="is-loading"><Loading /></el-icon>
              正在加载中...
            </div>
          </template>
        </el-table>
      </template>
    </div>
  </div>

  <div class="left-content" v-if="examStore.currQuestion.titleList.length && !justOne">
    <el-tabs v-model="activeTab" type="card" class="demo-tabs" @tab-click="handleClick">
      <el-tab-pane
        style="width: 100%"
        v-for="(tab, ind) in examStore.currQuestion.titleList.map((i) => i.name)"
        :label="tab"
        :name="ind"
      >
        <div
          class="content-box"
          v-if="!tableOfTitle"
          v-html="examStore.currQuestion.titleList[activeTab].title"
        ></div>

        <!-- 表格渲染 -->
        <div class="question-title" v-else-if="tableOfTitle.length">
          <template v-for="item in tableOfTitle">
            <span
              v-if="item !== '<p>{【表格插入位置标识】}</p>'"
              v-html="mathHTMLToShow(item)"
            ></span>
            <el-table
              v-else
              ref="excelRef"
              style="width: 31.75vw"
              :data="table?.[0]?.data?.slice(1)"
              border
            >
              <el-table-column
                :label="table?.[0]?.data?.[0]?.[it]"
                :prop="it"
                v-for="(it, ind) in table?.[0]?.filed"
                sortable
              />

              <template #empty>
                <div class="custom-loading">
                  <el-icon class="is-loading"><Loading /></el-icon>
                  正在加载中...
                </div>
              </template>
            </el-table>
          </template>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script setup>
import { ref, reactive, computed, onMounted, watch } from "vue";
import { Loading } from "@element-plus/icons-vue";
import { getQuestionExcel } from "@/service/exam";
import { useExam } from "@/stores/exam.js";
const examStore = useExam();

const activeTab = ref(0);
const justOne = computed(() => {
  return examStore.currQuestion.titleList.some((it) => it.id == 0);
});

const handleClick = (tab, event) => {
  activeTab.value = Number(tab.index);
  getQuestionExcelFn();
};

// 表格数据拆分
const table = ref([]);
const tableLoading = ref(true);
const tableOfTitle = computed(() => {
  if (
    !examStore.currQuestion.titleList[justOne.value ? 0 : activeTab.value].title.includes(
      "<p>{【表格插入位置标识】}</p>"
    )
  ) {
    return "";
  } else {
    return examStore.currQuestion.titleList[justOne.value ? 0 : activeTab.value].title
      .split(/(<p>\{【表格插入位置标识】\}<\/p>)/)
      .filter((segment) => segment.trim() !== "");
  }
});

// 获取表格数据
const getQuestionExcelFn = async () => {
  const data = {
    qid: examStore.currQuestion.pid,
    tid: justOne.value ? 0 : examStore.currQuestion.titleList[activeTab.value].id,
  };
  const res = await getQuestionExcel(data);
  if (res.code === "000000") {
    tableLoading.value = false;
    table.value = [
      {
        name: "表格",
        data: sortTable(processTable(res.data)),
        filed: Object.keys(sortTable(res.data)[0] || {}).filter((it) =>
          it.includes("field")
        ),
      },
    ];
  }
};

// 监听题目切换，做表格数据切换请求
watch(
  () => examStore.currQuestion?.id,
  () => {
    table.value = [];
    // 表格信息获取
    if (
      justOne.value &&
      examStore.currQuestion?.titleList?.[0]?.title.includes(
        "<p>{【表格插入位置标识】}</p>"
      )
    ) {
      getQuestionExcelFn();
    } else if (
      !justOne.value &&
      examStore.currQuestion?.titleList?.[activeTab.value]?.title.includes(
        "<p>{【表格插入位置标识】}</p>"
      )
    ) {
      getQuestionExcelFn();
    }
  },
  {
    immediate: true,
  }
);
</script>
<style lang="scss" scoped>
.left-content {
  flex: 1;
  padding-right: 10px;
  border-right: 1px solid #ccc;
  margin-right: 10px;
}

.demo-tabs {
  width: 100%;
}

.question-title {
  width: 100%;
}

table {
  width: 100%;
  border-collapse: collapse;
  border: none;
  tr {
    height: 32px;
  }

  th,
  td {
    border: 1px solid #dcdfe6;
    padding: 12px;
    text-align: center;

    &.option-text {
      text-align: left;
      min-width: 200px;
    }
  }

  th {
    background-color: #f5f7fa;
    font-weight: 500;
  }

  .el-radio {
    margin-right: 0;
  }
}
</style>
